package cn.org.mrliu.ssm.util;

import java.util.Arrays;

public class ArrayUtil {
	public static Object[][] split(Object[] objects, int interval) {
		if (objects == null || objects.length == 0) {
			return null;
		}
		if (interval == 0) {
			return null;
		}
		int length = objects.length;
		int part = length % interval == 0 ? length / interval : length / interval + 1;
		int fromIndex = 0; // 拆分的开始索引
		int toIndex = 0; // 拆分的结束索引
		// 新数组
		Object[][] newObjects = new Object[part][];
		for (int i = 0; i < part; i++) {
			// 如果是最后一部分,结束索引为集合size
			if (i + 1 == part) {
				fromIndex = i * interval;
				toIndex = length;
			} else {
				fromIndex = i * interval;
				toIndex = interval * (i + 1);
			}
			newObjects[i] = Arrays.copyOfRange(objects, fromIndex, toIndex);
		}
		return newObjects;
	}
}
